<template>
  <div class="toggle-container" :class="{ collapsed: collapsed }">
    <!--收缩图标-->
    <div class="toggle-container-header">
      <div v-show="!collapsed">
        <slot name="title">{{ title }}</slot>
      </div>
      <div class="toggle-control" @click="collapse">
        <div class="toggle-control-button">
          <toggle-icon :collapsed="collapsed"></toggle-icon>
        </div>
      </div>
    </div>
    <!--未收缩-->
    <div class="toggle-container-body-collapsed" v-show="collapsed">
      <div v-for="(name, index) in title.split('')" :key="`${name}-${index}`">
        {{ name }}
      </div>
    </div>
    <!--已收缩-->
    <div class="toggle-container-body" v-show="!collapsed">
      <div class="toggle-container-body-inner">
        <slot></slot>
      </div>
    </div>
  </div>
</template>

<script>
import ToggleIcon from '@/components/toggleContainer/ToggleIcon'

export default {
  name: 'ToggleContainer',
  components: {
    ToggleIcon
  },
  props: {
    title: {
      type: String,
      default: '标题'
    },
    onCollapse: {
      type: Function,
      default: () => {}
    }
  },
  data() {
    return {
      collapsed: false
    }
  },
  methods: {
    collapse() {
      const collapsed = !this.collapsed
      this.collapsed = collapsed
      this.onCollapse(collapsed)
    }
  }
}
</script>

<style scoped></style>
